using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Mysqlx.Crud;

namespace CPlatePrintSys.Win.Controls
{
    public class BaseInfoTreeNode : INotifyPropertyChanged
    {
        private bool? _isChecked = false;
        private bool _isExpanded;
        private bool _hasParentNode = false; // 是否是父节点选中状态
        private string _name;
        private ObservableCollection<BaseInfoTreeNode> _children;
        private BaseInfoTreeNode _parent;

        public string Name
        {
            get => _name;
            set { _name = value; OnPropertyChanged(nameof(Name)); }
        }
        public ObservableCollection<BaseInfoTreeNode> Children
        {
            get => _children;
            set { _children = value; OnPropertyChanged(nameof(Children)); }
        }
        public BaseInfoTreeNode Parent
        {
            get => _parent;
            set { _parent = value; OnPropertyChanged(nameof(Parent)); HasParentNode = (_parent != null ? true : false); }
        }

        public bool HasParentNode
        {
            get => _hasParentNode;
            set { _hasParentNode = value; OnPropertyChanged(nameof(HasParentNode)); }
        }

        
        public bool? IsChecked
        {
            get => _isChecked;
            set => SetIsChecked(value, true, true);
        }

        private void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
        {
            if (_isChecked != value)
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
                if (updateChildren && Children != null)
                {
                    foreach (var child in Children)
                    {
                        child.SetIsChecked(value, true, false); // 只向下，不向上
                    }
                }
                if (updateParent && Parent != null)
                {
                    Parent.VerifyCheckState();
                }
            }
        }
        public bool IsExpanded
        {
            get => _isExpanded;
            set { _isExpanded = value; OnPropertyChanged(nameof(IsExpanded)); }
        }
        public BaseInfoTreeNode(string name, BaseInfoTreeNode parent = null)
        {
            Name = name;
            Children = new ObservableCollection<BaseInfoTreeNode>();
            Parent = parent;
        }
        public void VerifyCheckState()
        {
            if (Children == null || Children.Count == 0)
                return;

            bool? state = Children[0].IsChecked;
            for (int i = 1; i < Children.Count; ++i)
            {
                if (Children[i].IsChecked != state)
                {
                    state = null; // 部分选中
                    break;
                }
            }
            SetIsChecked(state, false, true); // 只向上，不向下
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
} 